//
//  CinemaViewController.m
//  项目1 CLCMovie
//
//  Created by huiwenjiaoyu on 15/7/17.
//  Copyright (c) 2015年 CLC. All rights reserved.
//

#import "CinemaViewController.h"

@interface CinemaViewController ()
{
    NSMutableArray* _dataArray;
    NSMutableArray* _circleArray;
    NSMutableArray* _isOpened;
    UITableView* tab;

}
@end
//代理方法未写
@implementation CinemaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"影院"];
    self.view.backgroundColor = [UIColor redColor];
    [self _loadData];
    [self _creatTableView];

    // Do any additional setup after loading the view.
}


- (void)_creatTableView
{
    tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHieght-108) style:UITableViewStyleGrouped];
    tab.delegate = self;
    tab.dataSource = self;
    
    [tab registerNib:[UINib nibWithNibName:@"CinemaCellView" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cinemaCell"];
    [self.view addSubview:tab];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_dataArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if ([_isOpened[section] boolValue]) {
        NSLog(@"1");
    }
    else
        NSLog(@"2");
    return 3;
}


- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30)];
    [view setBackgroundColor:[UIColor grayColor]];
    
    
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 20)];
    label.text = [NSString stringWithFormat:@"%@",_circleArray[section]];
    label.textAlignment = NSTextAlignmentCenter;
    label.center = view.center;
    label.textColor = [UIColor whiteColor];
    
    [view addSubview:label];
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, kWidth, 30);
    btn.tag = section;
    [btn addTarget:self action:@selector(openOrClossSection:) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:btn];
    
    return view;
}

- (void)openOrClossSection:(UIButton*)btn
{
    NSString* str = _isOpened[btn.tag];
    if([str isEqualToString:@"YES"])
        str = @"NO";
    else
        str = @"YES";
    _isOpened[btn.tag] = str;
    
    [tab reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationTop];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CinemaCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cinemaCell" forIndexPath:indexPath];
    
    return cell;
}



#pragma mark - 数据加载
- (void)_loadData
{
    _dataArray = [[NSMutableArray alloc] init];
    NSDictionary* dic = [DataService getArrayOrDictionNaryfromJosnWithNamed:@"cinema_list"];
    NSArray* array = [NSArray arrayWithArray:[dic valueForKey:@"cinemaList"]];
    
    for(NSDictionary *dict in array)
    {
        CinemaModal* modal = [[CinemaModal alloc] init];
        [modal setValuesForKeysWithDictionary:dict];
        
        [_dataArray addObject:modal];
        
    }
    
    _circleArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < [_dataArray count]; i++) {
        
        NSString* circleName = [_dataArray[i] circleName];
        if (circleName) {
            [_circleArray addObject:circleName];
            
        }
    }

//    _isOpened = [[NSMutableArray alloc] init];
//    for (int i = 0; i < [_circleArray count]; i++) {
//        NSString* open = @"YES";
//        [_isOpened addObject:open];
//    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get t_dataArray	__NSArrayM *	@"142 objects"	0x00007f8de25c40c0he new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
